package portlet;

import javax.portlet.PortletMode;
import javax.portlet.WindowState;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

import oracle.jbo.common.JboCompOper;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;

import portlet.resource.soanphamBundle;

public class soanpham extends ADFBridgePortlet {
    private String langid;
    private String kieutapchiId;
    private String soid;
    private String urldanhsach;
    private String viewRender;
    //Labels
    private String labelSo;
    private String labelSoKhac;

    public String getViewRender() {
        getLangid();
        getKieutapchiId();
        getSoid();
        if (kieutapchiId == null && soid == null) {
            getSoMoiNhat();
        }
        getSoKhac();
        return viewRender;
    }

    public void getSoMoiNhat() {
        DCIteratorBinding somoiIterator =
            Utils.getIteratorBinding("AptcSoMoiNhat2View1Iterator");
        ViewObject vo = somoiIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("SoMoiViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem1 =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem1.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem1.setValue(langid);
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("SoMoiViewCriteria");
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
            soid = vo.getCurrentRow().getAttribute("Id").toString();
            kieutapchiId =
                    vo.getCurrentRow().getAttribute("KieutapchiId").toString();
        }
    }

    public void getSoKhac() {
        DCIteratorBinding sokhacIterator =
            Utils.getIteratorBinding("AptcSoMoiNhat2View1Iterator");
        ViewObject vo = sokhacIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("SoKhacViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem1 =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem1.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem1.setValue(langid);
        if (kieutapchiId != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem2 =
                vcRow.ensureCriteriaItem("KieutapchiId");
            rowViewCriteriaItem2.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem2.setValue(kieutapchiId.toString());
        }
        if (soid != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem3 =
                vcRow.ensureCriteriaItem("Id");
            rowViewCriteriaItem3.setOperator(JboCompOper.OPER_NE);
            rowViewCriteriaItem3.setValue(soid.toString());
        }
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("SoKhacViewCriteria");
        vo.applyViewCriteria(vc);
        vo.executeQuery();
    }

    public String getLangid() {
        langid = Utils.getPortalLocale("lang4").getLanguage();
        return langid;
    }

    public String getKieutapchiId() {
        try {
            kieutapchiId = Utils.getPortletRequest().getParameter("kieu");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return kieutapchiId;
    }

    public String getSoid() {
        try {
            soid = Utils.getPortletRequest().getParameter("so");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return soid;
    }

    public String getUrldanhsach() {
        try {
            urldanhsach =
                    Utils.getPortletRequest().getParameter("urldanhsach");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urldanhsach;
    }

    public String getLabelSo() {
        labelSo = Utils.getResource("SO", "lang4");
        return labelSo;
    }

    public String getLabelSoKhac() {
        labelSoKhac = Utils.getResource("TIEU_DE_SO_KHAC", "lang4");
        return labelSoKhac;
    }
}
